﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Address_Book.DAL
{
    public class User
    {
        public Address_Book.Model.UserInfo LoginSelect(string UserName, string PassWord)
        {
            //读取配置文件
            Properties.Settings conn = Properties.Settings.Default;

            //利用using 关键字，让cn自动释放
            using(SqlConnection cn = new SqlConnection())
            {
                cn.ConnectionString = conn.SqlConnection;
                cn.Open();
                SqlCommand cm = new SqlCommand();
                cm.Connection = cn;
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add("@UserName", SqlDbType.NVarChar);
                cm.Parameters.Add("@UserPassWord", SqlDbType.NVarChar);
                cm.CommandText = "p_LoginSelect";
                cm.Parameters["@UserName"].Value = UserName;
                cm.Parameters["@UserPassWord"].Value = PassWord;
                SqlDataReader reader = cm.ExecuteReader();
                Address_Book.Model.UserInfo user =null;
                while (reader.Read())
                {
                    if (user == null)
                    {
                        user = new Address_Book.Model.UserInfo();
                    }
                    user.UserID =reader.GetInt32(0);
                    user.UserName = reader.GetString(1);
                    if (!reader.IsDBNull(2))
                    {
                        user.PassWord = reader.GetString(2);
                    }
                }
                return user;
            }
        }
    }
}
